package cn.tedu._05mvcboot02.controller;

import cn.tedu._05mvcboot02.mapper.UserMapper;
import cn.tedu._05mvcboot02.pojo.entity.User;
import cn.tedu._05mvcboot02.pojo.userDTO.UserInsertDTO;
import cn.tedu._05mvcboot02.pojo.vo.UserListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

//@Controller
@RestController
@RequestMapping("/v1/users/")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @RequestMapping("insert")

    public String insert(UserInsertDTO userInsertDTO){
                /*
            梳理业务逻辑,确定接口;
            1.接收客户端传递的数据[用户名、密码、昵称];
            2.数据合法性校验;
            3.确定用户名是否被占用[SELECT接口];
              3.1 被占用,返回提示信息;
              3.2 未被占用,插入数据[INSERT接口],返回提示信息;
         */
        User user = new User();
        //复制属性
        BeanUtils.copyProperties(userInsertDTO, user);
        user.setCreated(new Date());

        userMapper.insertUser(user);
        return "添加成功";

    }

    @RequestMapping("userList")
    public List<UserListVO> userList(){
        return  userMapper.selectUserList();

    }

    @RequestMapping("delete")
    public  String delete(int id){
        userMapper.deleteUserById(id);
        return "删除成功";

    }

    @RequestMapping("update")
    public String update(User user){
        userMapper.updateById(user);
        return "修改成功";
    }
}
